<template>
  <div class="mod-config">
    <el-form :inline="true" :model="dataForm" @keyup.enter.native="getDataList()">
      <el-form-item>
        <el-input clearable placeholder="组编码" v-model="dataForm.groupCode"></el-input>
      </el-form-item>
      <el-form-item>
        <el-input clearable placeholder="名称" v-model="dataForm.dictName"></el-input>
      </el-form-item>
      <el-form-item>
        <el-button @click="newQueryList()" type="primary">查询</el-button>
        <el-button @click="addOrUpdateHandle()">新增</el-button>
        <!--        <el-button type="danger" @click="deleteHandle()" :disabled="dataListSelections.length <= 0">批量删除</el-button>-->
      </el-form-item>
    </el-form>
    <el-table
      :data="dataList"
      @selection-change="selectionChangeHandle"
      border
      style="width: 100%;"
      v-loading="dataListLoading">
      <!--      <el-table-column
              type="selection"
              header-align="center"
              align="center"
              width="50">
            </el-table-column>-->
      <el-table-column
        align="center"
        header-align="center"
        label="主键"
        prop="id"
        v-if="isShow">
      </el-table-column>
      <el-table-column
        align="center"
        header-align="center"
        label="组编码"
        prop="dictGroup">
      </el-table-column>

      <el-table-column
        align="center"
        header-align="center"
        label="名称"
        prop="dictLabel">
      </el-table-column>
      <el-table-column
        align="center"
        header-align="center"
        label="数据值"
        prop="dictValue">
      </el-table-column>
      <el-table-column
        align="center"
        header-align="center"
        label="样式"
        prop="dictStyle">
      </el-table-column>
      <el-table-column
        align="center"
        header-align="center"
        label="是否为默认值"
        prop="dictDefault">
      </el-table-column>

      <el-table-column
        align="center"
        header-align="center"
        label="描述"
        prop="dictDesc">
      </el-table-column>
      <el-table-column
        align="center"
        header-align="center"
        label="级联父ID"
        prop="dictPid">
      </el-table-column>
      <el-table-column
        align="center"
        header-align="center"
        label="顺序号"
        prop="orders">
      </el-table-column>
      <el-table-column
        align="center"
        fixed="right"
        header-align="center"
        label="操作"
        width="150">
        <template slot-scope="scope">
          <el-button @click="addOrUpdateHandle(scope.row.id, scope.row)" size="small" type="text">修改</el-button>
          <el-button @click="deleteHandle(scope.row.id)" size="small" type="text">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      :current-page="pageIndex"
      :page-size="pageSize"
      :page-sizes="[10, 20, 50, 100]"
      :total="totalPage"
      @current-change="currentChangeHandle"
      @size-change="sizeChangeHandle"
      layout="total, sizes, prev, pager, next, jumper">
    </el-pagination>
    <!-- 弹窗, 新增 / 修改 -->
    <add-or-update @refreshDataList="getDataList" ref="addOrUpdate" v-if="addOrUpdateVisible"></add-or-update>
  </div>
</template>

<script>
  import AddOrUpdate from './sysdictionary-add-or-update'

  export default {
    data() {
      return {
        isShow: false,
        dataForm: {
          key: ''
        },
        dataList: [],
        pageIndex: 1,
        pageSize: 10,
        totalPage: 0,
        dataListLoading: false,
        dataListSelections: [],
        addOrUpdateVisible: false
      }
    },
    components: {
      AddOrUpdate
    },
    // activated () {
    created() {
      this.getDataList()
    },
    methods: {
      newQueryList() {
        this.pageIndex = 1;
        this.pageSize = 10;
        this.getDataList();
      },
      // 获取数据列表
      getDataList() {
        this.dataListLoading = true
        let obj = {
          'page': this.pageIndex,
          'size': this.pageSize,
          'dictName': this.dataForm.dictName,
          'groupCode': this.dataForm.groupCode
        }
        obj = Object.assign(obj, this.dataForm)
        this.getTableList('/api/dictionary/list', obj, (data) => {
          if (data.code == '000000') {
            this.dataListLoading = false
            if (data.rows && data.rows.length > 0) {
              this.dataList = data.rows
              this.totalPage = data.total
            } else {
              this.dataList = []
              this.totalPage = 0
            }
          }
        })
      },
      // 每页数
      sizeChangeHandle(val) {
        this.pageSize = val
        this.pageIndex = 1
        this.getDataList()
      },
      // 当前页
      currentChangeHandle(val) {
        this.pageIndex = val
        this.getDataList()
      },
      // 多选
      selectionChangeHandle(val) {
        this.dataListSelections = val
      },
      // 新增 / 修改
      addOrUpdateHandle(id, row) {
        this.addOrUpdateVisible = true
        this.$nextTick(() => {
          this.$refs.addOrUpdate.init(id, row)
        })
      },
      // 删除
      deleteHandle(id) {
        var ids = id ? [id] : this.dataListSelections.map(item => {
          return item.id
        })
        this.$confirm(`确定对[` + ids.length + `]个记录，进行删除操作吗?`, '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          this.$http({
            url: this.$http.adornUrl('/api/sysDictionaryDel'),
            method: 'post',
            data: this.$http.adornData({'id': ids[0]})
          }).then(({data}) => {
            if (data && data.code === '000000') {
              this.$message({
                message: '操作成功',
                type: 'success',
                duration: 1500,
                onClose: () => {
                  this.getDataList()
                }
              })
            } else {
              this.$message.error(data.msg)
            }
          })
        })
      }
    }
  }
</script>
